package cn.lovemmd.model.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理通用异常
     */
    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception e) {
        System.err.println("系统异常: " + e.getMessage());
        e.printStackTrace();
        return Result.error("系统内部错误，请联系管理员");
    }

    /**
     * 处理运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<String> handleRuntimeException(RuntimeException e) {
        System.err.println("运行时异常: " + e.getMessage());
        e.printStackTrace();
        return Result.error("操作失败: " + e.getMessage());
    }

    /**
     * 处理参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<String> handleIllegalArgumentException(IllegalArgumentException e) {
        System.err.println("参数异常: " + e.getMessage());
        return Result.error("参数错误: " + e.getMessage());
    }
}
